import {
	getUserInfo
} from "@/api/Interface/user"
import Store from "@/store"
import BasicConfig from "@/utils/config"
import {
	UserInfoPerfectEnum
} from "../enums/common";

export const useGetUserInfo = async () => {
	const userId = Store.state.user.userId;
	await getUserInfo({
		userId
	}).then(res => {
		if (res.code === 200) {
			Store.commit("user/SET_USER", res.data)
			useSetRoles(res.data?.roles ?? [])
			// 更新用户完善资料信息
			useUpdUserPerfect(res.data?.bs)
		} else BasicConfig.toast(res.msg)
		return Promise.resolve(res)
	}).catch(err => {
		return Promise.reject(err)
	})
}

export const useUpdUserPerfect = (param) => {
	Store.commit('user/SET_PERFECT', param === UserInfoPerfectEnum["已完善"]);
}

export const useSetRoles = (roles) => {
	Store.commit('user/SET_ROLES', roles);
}